package cn.tedu.knows.portal.service.impl;

import cn.tedu.knows.portal.exception.ServiceException;
import cn.tedu.knows.portal.mapper.QuestionMapper;
import cn.tedu.knows.portal.mapper.UserMapper;
import cn.tedu.knows.portal.model.Answer;
import cn.tedu.knows.portal.mapper.AnswerMapper;
import cn.tedu.knows.portal.model.Question;
import cn.tedu.knows.portal.model.User;
import cn.tedu.knows.portal.service.IAnswerService;
import cn.tedu.knows.portal.vo.AnswerVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2021-11-26
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public Answer saveAnswer(AnswerVO answerVO, String username) {
        User user = userMapper.findUserByUsername(username);
        Answer answer = new Answer()
                .setContent(answerVO.getContent())
                .setQuestId(answerVO.getQuestionId())
                .setUserNickName(user.getNickname())
                .setUserId(user.getId())
                .setLikeCount(0)
                .setAcceptStatus(0)
                .setCreatetime(LocalDateTime.now());
        int num = answerMapper.insert(answer);
        if (num != 1) {
            throw new ServiceException("数据库忙");
        }
        // 千万别忘了返回answer
        return answer;
    }

    @Override
    public List<Answer> getAnswerByQuestionId(Integer questionId) {
        List<Answer> answers = answerMapper.findAnswersByQuestionId(questionId);
        return answers;
    }

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    // 方法中有两次修改操作,必须添加事务管理
    @Transactional
    public boolean accept(Integer answerId, String username) {
        User user = userMapper.findUserByUsername(username);
        //根据answerId 查询出answer对象
        Answer answer = answerMapper.selectById(answerId);
        //再根据answer对象中的questId查询出question对象
        Question question = questionMapper.selectById(answer.getQuestId());
        //判断当前登录用户是不是问题的发布者
        if (user.getId().equals(question.getUserId())) {
            // 先修改回答answer的状态
            int num = answerMapper.updateAcceptStatus(1, answerId);
            if (num != 1) {
                throw new ServiceException("数据库忙");
            }
            num = questionMapper.updateStatus(Question.SOLVED, question.getId());
            if (num != 1) {
                throw new ServiceException("数据库忙");
            }
            return true;
        }
        return false;
    }
}
